package com.lzs.adx.web.modules.adx.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lzs.adx.commmon.page.PageData;
import com.lzs.adx.commmon.service.impl.CrudServiceImpl;
import com.lzs.adx.web.modules.adx.dao.AccountReportMonthDao;
import com.lzs.adx.web.modules.adx.dto.AccountReportDayDTO;
import com.lzs.adx.web.modules.adx.dto.AccountReportMonthDTO;
import com.lzs.adx.web.modules.adx.entity.AccountReportMonthEntity;
import com.lzs.adx.web.modules.adx.service.AccountReportMonthService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author wangzhijun 825400558@qq.com
 * @since 1.0.0 2024-09-03
 */
@Service
public class AccountReportMonthServiceImpl extends CrudServiceImpl<AccountReportMonthDao, AccountReportMonthEntity, AccountReportMonthDTO> implements AccountReportMonthService {

    @Override
    public QueryWrapper<AccountReportMonthEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String type = (String)params.get("type");

        QueryWrapper<AccountReportMonthEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(type), "type", type);

        return wrapper;
    }


    @Override
    public PageData<AccountReportMonthDTO> findPage(Map<String, Object> params) {
//        IPage<AccountReportMonthDTO> listPage = baseDao.selectMonthReportPage(getPage(params, null, false), params);

//        return new PageData<>(listPage.getRecords(), listPage.getTotal());
        return null;
    }

    @Override
    public void generateData(AccountReportMonthDTO accountReportDayDTO) {
        LambdaQueryWrapper<AccountReportMonthEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AccountReportMonthEntity::getDspId,accountReportDayDTO.getDspId());
        lambdaQueryWrapper.eq(AccountReportMonthEntity::getMonth,accountReportDayDTO.getMonth());
        List<AccountReportMonthEntity> accountReportMonthEntities = baseDao.selectList(lambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(accountReportMonthEntities)){
            //更新
            AccountReportMonthEntity dbAccountMonth = accountReportMonthEntities.get(0);
            accountReportDayDTO.setId(dbAccountMonth.getId());
            accountReportDayDTO.setUpdateTime(new Date());
            this.update(accountReportDayDTO);
        }else {
           //新增
            accountReportDayDTO.setCreateTime(new Date());
            accountReportDayDTO.setUpdateTime(new Date());
            //广告业务
            this.save(accountReportDayDTO);
        }
    }


}